package ch.ethz.inf.shop.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;

import ch.ethz.inf.shop.Title;
import ch.ethz.inf.shop.model.simpleDatabase.Tuple;

public class Comment {

	@Title(text = "User", isVisible = true)
	private final RegisteredUser user;

	@Title(text = "Date", isVisible = true)
	private final Date date;

	@Title(text = "Comment", isVisible = true)
	private final String text;
	@Title(text = "Comment ID", isVisible = false)
	private final int commentId;

	@Title(text = "Product", isVisible = false)
	private Product product;

	public String getText() {
		return text;
	}

	public int getCommentId() {
		return commentId;
	}

	public Product getProduct() {
		return product;
	}

	public RegisteredUser getUser() {
		return user;
	}

	public Date getDate() {
		return date;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Comment(String text, int commentId, Product product, RegisteredUser user, Date date) {
		this.text = text;
		this.commentId = commentId;
		this.product = product;
		this.user = user;
		this.date = date;
	}

	public Comment(ResultSet rs) throws SQLException {
		this.text = rs.getString("text");
		this.date = rs.getDate("date");
		this.commentId = rs.getInt("comment_id");
		this.user = new RegisteredUser(rs);
	}

	public Comment(Tuple rs) throws ParseException {
		this.text = rs.getString("comment.text");
		this.date = rs.getDate("comment.date");
		this.commentId = rs.getInt("comment.comment_id");
		this.user = new RegisteredUser(rs);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + commentId;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + ((product == null) ? 0 : product.hashCode());
		result = prime * result + ((text == null) ? 0 : text.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Comment other = (Comment) obj;
		if (commentId != other.commentId)
			return false;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (product == null) {
			if (other.product != null)
				return false;
		} else if (!product.equals(other.product))
			return false;
		if (text == null) {
			if (other.text != null)
				return false;
		} else if (!text.equals(other.text))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}

}
